const app = getApp()
const originpromote = [
    { label: "分销中心", icon: "team", route: "./sale/home" },
    { label: "推广联盟", icon: "address", route: "./team/vip" }
]
Page({
    data: {
        defaultAvatar: "http://img.zhxcxmall.com/static/default-avatar3.png",
        order_entry: [
            { label: '待支付', icon: 'dfk', status: 0 },
            { label: '待成团', icon: 'dct', status: 70 },
            { label: '待发货', icon: "dfh", status: 20 },
            { label: '待收货', icon: "dsh", status: 40 },
            { label: '待评价', icon: "dpj", status: 60 },
        ],
        extras: [
            { label: "我的收藏", icon: "keep", route: "./keep/keep" },
            { label: "领取优惠券", icon: "coupons", route: "./coupons/coupons" },
            { label: "我的优惠券", icon: "mycoupons", route: "./coupons/my-coupons" },            
            // { label: "我的名片", icon: "card", route: "/pages/personal/card/my-card" },
            // { label: "收货地址", icon: "address", route: "/pages/address/address" },
            // { label: "钱包", icon: "wallet", route: "./wallet/wallet" },
        ]
    },
    onShow: function () {
        wx.showNavigationBarLoading()
        app.http.get("member/index").then(result => {
            wx.hideLoading()
            this.setData({
                "order_entry[0].num": result.data.orderNoPay,
                "order_entry[1].num": result.data.orderGroup,
                "order_entry[2].num": result.data.orderToSend,
                "order_entry[3].num": result.data.orderSign,
                "order_entry[4].num": result.data.orderComment,
                levelName: result.data.memberClass,
                promote: originpromote.filter(o => {
                    if (o.label == "分销中心") {
                        return result.data.showSale
                    } else if (o.label == "推广联盟") {
                        return result.data.showBuyVip
                    }
                    return true
                })
            })
            wx.hideNavigationBarLoading()
        }).catch(err => {
            wx.hideNavigationBarLoading()
            app.wxToast.error(err.message)
        })

        app.toPromise(wx.getStorage, { key: "user" }).then(res => {
            return { userInfo: res.data }
        }).catch(err => {
            return app.toPromise(wx.getUserInfo)
        }).then(res => {
            if (res.userInfo) {
                this.setData({
                    user: res.userInfo
                })
                app.http.post("member/save-info", {
                    data: res.userInfo
                })
                wx.setStorage({
                    key: 'user',
                    data: res.userInfo,
                })
            }

        }).catch(err => {

        })
    },

    getUser(evt) {
        const user = evt.detail.userInfo
        if (user) {
            wx.setStorage({
                key: 'user',
                data: user,
            })
            this.setData({
                user
            })
            app.http.post("member/save-info", {
                data: user
            })
        }
    },
    viewAddress() {
        wx.chooseAddress()
    }
})